package com.dennytech.tac.base;

import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

import com.dennytech.tac.R;

public class CFFragment extends Fragment {

	protected TextView titleText;
	protected ImageButton leftTitleButton;
	protected TextView leftTitleTextButton;
	protected ImageButton rightTitleButton;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		titleText = (TextView) getView().findViewById(android.R.id.title);
		leftTitleTextButton = (TextView) getView().findViewById(
				R.id.left_title_text_button);
		leftTitleButton = (ImageButton) getView().findViewById(
				R.id.left_title_button);
		rightTitleButton = (ImageButton) getView().findViewById(
				R.id.right_title_button);
	}

	protected void setTitle(String text) {
		if (titleText == null) {
			return;
		}

		titleText.setText(text);
		titleText.setVisibility(View.VISIBLE);
	}

	protected void setLeftTitleButton(int resId) {
		setLeftTitleButton(resId, null);
	}

	protected void setLeftTitleButton(int resId, OnClickListener listener) {
		if (leftTitleButton == null) {
			return;
		}

		leftTitleButton.setImageResource(resId);
		leftTitleButton.setOnClickListener(listener);
		leftTitleButton.setVisibility(resId == 0 ? View.GONE : View.VISIBLE);
		if (leftTitleTextButton != null) {
			leftTitleTextButton.setVisibility(View.GONE);
		}
	}

	protected void setLeftTitleTextButton(String text) {
		setLeftTitleTextButton(text, null);
	}

	protected void setLeftTitleTextButton(String text, OnClickListener listener) {
		if (leftTitleTextButton == null) {
			return;
		}

		leftTitleTextButton.setText(text);
		leftTitleTextButton.setOnClickListener(listener);
		leftTitleTextButton.setVisibility(text == null ? View.GONE
				: View.VISIBLE);
		if (leftTitleButton != null) {
			leftTitleButton.setVisibility(View.GONE);
		}
	}

	protected void setRightTitleButton(int resId) {
		setLeftTitleButton(resId, null);
	}

	protected void setRightTitleButton(int resId, OnClickListener listener) {
		if (rightTitleButton == null) {
			return;
		}

		rightTitleButton.setImageResource(resId);
		rightTitleButton.setOnClickListener(listener);
		rightTitleButton.setVisibility(resId == 0 ? View.GONE : View.VISIBLE);
	}

	public Dialog createDialog(int menuResId,
			android.content.DialogInterface.OnClickListener l) {
		Dialog dialog = null;
		Builder builder = new android.app.AlertDialog.Builder(getActivity());
		builder.setItems(menuResId, l);
		dialog = builder.create();
		return dialog;
	}

	public static SharedPreferences preferences(Context c) {
		return c.getSharedPreferences(c.getPackageName(),
				Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
	}

}
